package com.educal.Controller;

import com.educal.Bean.Admin;
import com.educal.Repository.AdminRepository;
import com.educal.Repository.StuRepository;
import com.educal.Bean.Student;
import com.educal.Bean.Teacher;
import com.educal.Repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by miyang on 2017/11/23.
 */
@RestController
public class UserController {

    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private StuRepository stuRepository;
    @Autowired
    private TeacherRepository teacherRepository;

    //  Session session = new HttpSession();
    /*
    * 在界面获取用户id、密码、权限与数据库中的比较，
    * 成功返回200，错误返回401
    * */

    @PostMapping("/users")
    public Map<String, Integer> login(@RequestParam("usrid") String usrid,
                                      @RequestParam("pwd") String pwd,
                                      @RequestParam("auth") Integer auth,
                                        HttpSession session) {
        Integer flag = 400;//表示登录失败
        Map<String, Integer> result = new HashMap<String, Integer>();
        result.put("code", flag);
        //System.out.print(usrid + pwd + auth);
        if (auth.equals(1)) {
            Admin admin = new Admin();
            List<Admin> list = adminRepository.findAll();
            for (int i = 0; i < list.size(); i++) {
                String userId = list.get(i).getAdminid();
                String userPwd = list.get(i).getAdminpassword();
                if (usrid.equals(userId) && pwd.equals(userPwd)) {
                    flag = 200;
                    result.put("code", flag);
                    /*session.setAttribute("id",id);
                    session.setAttribute("password",password);
                    session.setAttribute("auth",auth);*/
                    session.setAttribute("result", result);
                    break;
                }
            }
        } else if (auth.equals(2)) {
            List<Teacher> list = teacherRepository.findAll();
            for (int i = 0; i < list.size(); i++) {
                String userId = list.get(i).getTid();
                String userPwd = list.get(i).getTpassword();

                //   System.out.print(userId + userPwd + userAuth);
                if (usrid.equals(userId) && pwd.equals(userPwd)) {
                    flag = 200;
                    result.put("code", flag);
                   /*session.setAttribute("id",id);
                  session.setAttribute("password",password);
                   session.setAttribute("auth",auth);*/
                    session.setAttribute("result", result);
                    break;
                }
            }
        } else if (auth.equals(3)) {
            List<Student> list = stuRepository.findAll();
            for (int i = 0; i < list.size(); i++) {
                String userId = list.get(i).getStuid();
                String userPwd = list.get(i).getStupassword();

                //   System.out.print(userId + userPwd + userAuth);
                if (usrid.equals(userId) && pwd.equals(userPwd)) {
                    flag = 200;
                    result.put("code", flag);
                   /*session.setAttribute("id",id);
                   session.setAttribute("password",password);
                   session.setAttribute("auth",auth);*/
                    session.setAttribute("result", result);
                    break;
                }
            }
        }
        return result;
    }

    @PutMapping("/users")
    public Map<String, Integer> logout(HttpSession session) {
        Map<String, Integer> result = new HashMap<String, Integer>();
        if(session.getAttribute("result")!=null){
            session.removeAttribute("result");
            result.put("code", 200);
        }else {
            result.put("code", 400);
        }

        return result;
    }
}
